<template>
  <div class="container">
    <el-card class="box-card">
      <div class="clearfix">
        <span>说明：目前支持学科和关键字条件筛选</span>
        <el-button
          style="float: right"
          type="success"
          size="medium"
          icon="el-icon-edit"
          @click="$router.push({ name: 'questions-new' })"
          >新增试题</el-button
        >
      </div>
      <div class="text item">
        <el-form
          :label-position="labelPosition"
          label-width="80px"
          size="small"
        >
          <el-row :gutter="20">
            <el-col :span="6">
              <el-form-item label="学科">
                <el-select
                  v-model="form.subjectID"
                  placeholder="请选择"
                  @focus="subjectsList"
                >
                  <el-option
                    v-for="item in getsubjectsList"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="二级目录">
                <el-select
                  v-model="form.catalogID"
                  placeholder="请选择"
                  @focus="twoSubjectsList"
                >
                  <el-option
                    v-for="item in gettwoSubjectsList"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="标签">
                <el-select
                  v-model="form.tags"
                  placeholder="请选择"
                  @focus="tagsList"
                >
                  <el-option
                    v-for="item in gettagsList"
                    :key="item.id"
                    :label="item.tagName"
                    :value="item.id"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="关键字">
                <el-input
                  v-model="form.keyword"
                  placeholder="根据题干搜索"
                ></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="试题类型">
                <el-select v-model="form.questionType" placeholder="请选择">
                  <el-option
                    v-for="item in questionType"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="难度">
                <el-select v-model="form.difficulty" placeholder="请选择">
                  <el-option
                    v-for="item in difficulty"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="方向">
                <el-select v-model="form.direction" placeholder="请选择">
                  <el-option
                    v-for="(item, index) in direction"
                    :key="index"
                    :label="item"
                    :value="item"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="录入人">
                <el-select v-model="form.creatorID" placeholder="请选择">
                  <el-option label="超级管理员" value="2"></el-option>
                  <el-option label="录入管理员" value="3"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="题目备注">
                <el-input v-model="form.remarks"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="企业简称">
                <el-input v-model="form.shortName"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="城市">
                <el-row>
                  <el-col :span="12">
                    <el-select
                      v-model="form.province"
                      placeholder="请选择"
                      @focus="provinces"
                    >
                      <el-option
                        v-for="(item, index) in getprovinces"
                        :key="index"
                        :label="item"
                        :value="item"
                      ></el-option>
                    </el-select>
                  </el-col>
                  <el-col :span="12">
                    <el-select
                      v-model="form.city"
                      placeholder="请选择"
                      @focus="citys"
                    >
                      <el-option
                        v-for="(item, index) in getcitys"
                        :key="index"
                        :label="item"
                        :value="item"
                      ></el-option>
                    </el-select>
                  </el-col>
                </el-row>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item style="text-align: right">
                <el-button @click="clearForm">清除</el-button>
                <el-button type="primary" @click="search">搜索</el-button>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
      <el-alert :title="`共有${page.total}条数据`" type="info" show-icon>
      </el-alert>
      <el-table
        :data="tableData"
        style="width: 100%"
        size="medium"
        v-loading="loading"
      >
        <el-table-column prop="number" label="试题编号"> </el-table-column>
        <el-table-column prop="subject" label="学科"> </el-table-column>
        <el-table-column prop="catalog" label="目录"> </el-table-column>
        <el-table-column prop="questionType" label="题型"> </el-table-column>
        <el-table-column prop="question" label="题干" width="200">
          <template slot-scope="scope">
            <div v-html="scope.row.question"></div>
          </template>
        </el-table-column>
        <el-table-column prop="addDate" label="录入时间" width="200">
        </el-table-column>
        <el-table-column prop="difficulty" label="难度"> </el-table-column>
        <el-table-column prop="creator" label="录入人"> </el-table-column>
        <el-table-column prop="name" label="操作" min-width="120px">
          <template slot-scope="scope">
            <el-row :gutter="5">
              <el-col :span="6">
                <el-button
                  type="primary"
                  icon="el-icon-view"
                  circle
                  :plain="true"
                  size="small"
                  @click="preview(scope.row)"
                ></el-button>
              </el-col>
              <el-col :span="6">
                <el-button
                  type="success"
                  icon="el-icon-edit"
                  circle
                  :plain="true"
                  size="small"
                  @click="
                    $router.push({
                      name: 'questions-new',
                      query: { id: scope.row.id },
                    })
                  "
                ></el-button> </el-col
              ><el-col :span="6">
                <el-button
                  type="danger"
                  icon="el-icon-delete"
                  circle
                  :plain="true"
                  size="small"
                  @click="removeList(scope.row)"
                ></el-button> </el-col
              ><el-col :span="6">
                <el-button
                  type="warning"
                  icon="el-icon-check"
                  circle
                  :plain="true"
                  size="small"
                  @click="choiceAdd(scope.row)"
                ></el-button>
              </el-col>
            </el-row>
          </template>
        </el-table-column>
      </el-table>
      <el-row>
        <el-col style="text-align: right">
          <el-pagination
            background
            layout=" prev, pager, next,sizes, jumper"
            :page-sizes="[5, 10, 20, 50]"
            :page-size="page.pagesize"
            :current-page="page.page"
            :total="page.total"
            @current-change="changePage"
            @size-change="changeSize"
          >
          </el-pagination>
        </el-col>
      </el-row>
    </el-card>
    <Preview
      :dialogVisible="dialogVisible"
      :onlist="onlist"
      ref="findRadio"
      @close="closePreview($event)"
    />
  </div>
</template>

<script>
import { list, detail, remove, choiceAdd } from '@/api/hmmm/questions'
// 学科列表
import { simple as subjectsList } from '@/api/hmmm/subjects'
import { simple as subjectsDetail } from '@/api/hmmm/directorys'
// 标签列表
import { list as tagsList } from '@/api/hmmm/tags'
// 城市
import { provinces, citys } from '@/api/hmmm/citys'
// 类型其他数据 难度 题型 方向
import { difficulty, questionType, direction } from '@/api/hmmm/constants'
// 引入事件过滤器
import { parseTimeByString } from '@/filters'
// 引入试题预览组件
import Preview from '../components/questions-preview.vue'
export default {
  components: { Preview },
  data () {
    return {
      labelPosition: 'right',
      loading: false,
      form: {
        subjectID: '',
        catalogID: '',
        tags: '',
        keyword: '',
        questionType: '',
        difficulty: '',
        direction: '',
        creatorID: '',
        remarks: '',
        shortName: '',
        province: '', // 城市
        city: '' // 城市下的区域
      },
      difficulty, // 难度
      questionType, // 题型
      direction, // 方向
      getsubjectsList: [],
      gettwoSubjectsList: [],
      gettagsList: [],
      getprovinces: [], // 城市
      getcitys: [], // 城市下的区域
      tableData: [],
      page: {
        page: 1, // 当前页码
        pagesize: 5,
        total: 0
      },
      dialogVisible: false, // 控制预览弹窗
      onlist: {} // 当前点击行
    }
  },
  methods: {
    async list (form) {
      // 加载中
      this.loading = true
      const obj = { ...form, ...this.page }
      try {
        const {
          data: { items, counts }
        } = await list(obj)
        this.page.total = counts
        items.forEach((item) => {
          if (item.questionType === '1') {
            item.questionType = '单选'
          } else {
            item.questionType = '多选'
          }
          if (item.difficulty === '1') {
            item.difficulty = '简单'
          } else {
            item.difficulty = '困难'
          }
          item.addDate = parseTimeByString(item.addDate)
        })
        this.tableData = items
        // 获取完数据关闭加载
        this.loading = false
      } catch (error) {
        console.log(error)
      }
    },
    changePage (newPage) {
      this.page.page = newPage
      this.list()
    },
    changeSize (size) {
      console.log(size)
      this.page.pagesize = size
      this.page.page = 1
      this.list()
    },
    async preview (onlist) {
      try {
        // console.log(onlist)
        const { data } = await detail(onlist)
        this.onlist = data
        if (data.questionType === '1') {
          this.onlist.questionType = '单选'
        } else {
          this.onlist.questionType = '多选'
        }
        if (data.difficulty === '1') {
          this.onlist.difficulty = '简单'
        } else {
          this.onlist.difficulty = '困难'
        }
        this.$refs.findRadio.findRadio()
      } catch (error) {
        console.log(error)
      }
      // 先获取数据再打开弹窗 避免数据闪烁
      this.dialogVisible = true
    },
    closePreview (event) {
      this.dialogVisible = event
      // 关闭弹窗之后清空数据
      this.onlist = {}
    },
    // 删除数据
    removeList (onlist) {
      this.$confirm('确定删除该数据吗?', {
        type: 'warning'
      })
        .then(async () => {
          try {
            await remove(onlist)
            // 执行完删除接口之后重新更新数据
            this.list()
            this.$message.success('删除成功!')
          } catch (error) {
            console.log(error)
          }
        })
        .catch(() => {})
    },
    // 加入精选
    choiceAdd (onlist) {
      // 加入精选需要传选择参数choiceState===1是添加
      const choiceOnlist = onlist
      choiceOnlist.choiceState = 1
      console.log(choiceOnlist)
      this.$confirm('确定加入精选题库吗?', {
        type: 'warning'
      })
        .then(async () => {
          try {
            await choiceAdd(choiceOnlist)
            // 执行完删除接口之后重新更新数据
            this.list()
            this.$message.success('添加精选题库成功!')
          } catch (error) {
            console.log(error)
          }
        })
        .catch(() => {})
    },
    // 学科列表
    async subjectsList () {
      const { data } = await subjectsList()
      this.getsubjectsList = data
    },
    // 二级列表
    async twoSubjectsList () {
      if (this.form.subjectID) {
        const { data } = await subjectsDetail({
          subjectID: this.form.subjectID
        })
        this.gettwoSubjectsList = data
      }
    },
    // 标签列表
    async tagsList () {
      const {
        data: { items }
      } = await tagsList()
      this.gettagsList = items
    },
    // 城市
    provinces () {
      this.getprovinces = provinces()
      this.form.city = ''
    },
    citys () {
      this.getcitys = citys(this.form.province)
    },
    clearForm () {
      this.form = {
        subjectID: '',
        catalogID: '',
        tags: '',
        keyword: '',
        questionType: '',
        difficulty: '',
        direction: '',
        creatorID: '',
        remarks: '',
        shortName: '',
        province: '', // 城市
        city: '' // 城市下的区域
      }
    },
    search () {
      const searchData = {}
      for (const item in this.form) {
        // console.log(this.form[item])
        if (this.form[item]) {
          // 如果输入框中的数据不是空
          searchData[item] = this.form[item]
        }
      }
      console.log(searchData)
      if (searchData) {
        this.list(searchData)
      } else {
        this.list()
      }
    }
  },
  created () {
    this.list()
  }
}
</script>

<style scoped lang='scss'>
.container {
  padding-top: 10px;
  padding-left: 10px;
  .box-card {
    .clearfix {
      margin-bottom: 20px;
      span {
        color: pink;
        font-size: 12px;
      }
      .el-button {
        font-size: 12px;
        border-radius: 3px;
      }
    }
    .el-form-item {
      .el-form-item__content {
        .el-select {
          width: 100%;
        }
      }
    }
  }
}
</style>
